#include "RenameDialog.hpp"
#include <errno.h>

BEGIN_EVENT_TABLE(RenameDialog, wxDialog)
EVT_BUTTON(ID_DialogOkButton, RenameDialog::renameFile)
EVT_BUTTON(ID_DialogCloseButton, RenameDialog::close)
END_EVENT_TABLE()

RenameDialog::RenameDialog(const wxString &title, std::string ifile, std::string ipath, ClientCommands icc, MainFrame *iparent)
: wxDialog(NULL, -1, title, wxDefaultPosition, wxSize(250, 100), wxTAB_TRAVERSAL) {
    parent = iparent;
    toServer = true;
    cc = icc;
    path = ipath;
    file = path;
    file += ifile;
    std::string strtemp = std::string(title.mbc_str()) + ": " + ifile;
    new wxStaticText(this, -1, wxString(strtemp.c_str(), wxConvUTF8),
            wxPoint(5, 5), wxDefaultSize);
    tc = new wxTextCtrl(this, ID_RenameTextBox, wxString(ifile.c_str(), wxConvUTF8),
            wxPoint(5, 10 + DEFAULTTEXTBOXHEIGHT), wxSize(DEFAULTTEXTBOXWIDTH, DEFAULTTEXTBOXHEIGHT));

    okButton = new wxButton(this, ID_DialogOkButton, wxT("Ok"),
            wxPoint(5, 10 + DEFAULTTEXTBOXHEIGHT * 2), wxSize(DEFAULTBUTTONWIDTH, DEFAULTBUTTONHEIGHT));
    closeButton = new wxButton(this, ID_DialogCloseButton, wxT("Close"),
            wxPoint(5 + DEFAULTBUTTONWIDTH, 10 + DEFAULTTEXTBOXHEIGHT * 2), wxSize(DEFAULTBUTTONWIDTH, DEFAULTBUTTONHEIGHT));

    tc->SetFocus();
    Centre();
    ShowModal();
    Destroy();
}

RenameDialog::RenameDialog(const wxString &title, std::string ifile, std::string ipath, MainFrame *iparent)
: wxDialog(NULL, -1, title, wxDefaultPosition, wxSize(250, 100)) {
    parent = iparent;
    toServer = false;
    path = ipath;
    file = path;
    file += ifile;
    std::string strtemp = std::string(title.mbc_str()) + ": " + ifile;
    new wxStaticText(this, -1, wxString(strtemp.c_str(), wxConvUTF8),
            wxPoint(5, 5), wxDefaultSize);
    tc = new wxTextCtrl(this, ID_RenameTextBox, wxString(ifile.c_str(), wxConvUTF8),
            wxPoint(5, 10 + DEFAULTTEXTBOXHEIGHT), wxSize(DEFAULTTEXTBOXWIDTH, DEFAULTTEXTBOXHEIGHT));

    okButton = new wxButton(this, ID_DialogOkButton, wxT("Ok"),
            wxPoint(5, 10 + DEFAULTTEXTBOXHEIGHT * 2), wxSize(DEFAULTBUTTONWIDTH, DEFAULTBUTTONHEIGHT));
    closeButton = new wxButton(this, ID_DialogCloseButton, wxT("Close"),
            wxPoint(5 + DEFAULTBUTTONWIDTH, 10 + DEFAULTTEXTBOXHEIGHT * 2), wxSize(DEFAULTBUTTONWIDTH, DEFAULTBUTTONHEIGHT));

    tc->SetFocus();
    Centre();
    ShowModal();
    Destroy();
}

void RenameDialog::renameFile(wxCommandEvent& event) {
    std::string newName = path;
    newName += tc->GetValue().mbc_str();
    if (toServer == true) {
        if (!parent->checkExists(std::string(tc->GetValue().mbc_str()), parent->serverList)) {
            int rCode = 0;
            if ((rCode = cc.requestRename(file, newName.c_str())) != 0) {
                parent->errorBox(rCode);
            }
        }
    } else {
        if (!parent->checkExists(std::string(tc->GetValue().mbc_str()), parent->clientList)) {
            if (rename(file.c_str(), newName.c_str()) != 0) {
                parent->errorBoxClient(errno);
            }
        }
    }
    Destroy();
}

void RenameDialog::close(wxCommandEvent& event) {
    Destroy();
}
